package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.OverdueCouponAdapter;
import com.luck.easyfoodbatch.dialog.ShowProgressDialog;
import com.luck.easyfoodbatch.mvp.mine.MyVouchersModel;
import com.luck.easyfoodbatch.mvp.mine.MyVouchersPresenter;
import com.luck.easyfoodbatch.mvp.mine.MyVouchersView;
import com.luck.easyfoodbatch.mvp.model.MyCouponModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OverdueCouponActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, MyVouchersView {

    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private OverdueCouponAdapter adapter;
    private List<MyCouponModel.DataBean.ModelsBean> list = new ArrayList<>();
    private MyVouchersModel myVouchersModel;
    private int totalPage = 1;
    private int page = 1;
    private int isRefresh = 0;// 0普通请求，1下拉刷新，2上拉加载
    private int effective = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overdue_coupon, "已过期优惠券", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(manager);
        adapter = new OverdueCouponAdapter(mContext);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {

            }
        });
        showProgressDialog(getString(R.string.app_loading));
        myVouchersModel = new MyVouchersPresenter(this, mContext);
        myVouchersModel.myVouchers(effective, page, 0, "");
        adapter.addAll(list);
        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshListener(this);
    }

    @Override
    public void onLoadMore() {
        //刷新
        isRefresh = 1;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }
        myVouchersModel.myVouchers(effective, page, 0, "");
    }

    @Override
    public void onRefresh() {
        isRefresh = 0;
        page = 1;
        myVouchersModel.myVouchers(effective, page, 0, "");
    }

    @Override
    public void loginMyVouchers(MyCouponModel model) {
        if (model.getStatus() == HttpUtil.RESULT_OK) {
            totalPage = model.getData().getTotalPage();
            // 下拉刷新 清空集合
            switch (isRefresh) {
                case 0:
                    list.clear();
                    list = model.getData().getModels();
                    break;
                case 1:
                    // 下拉刷新
                    list.addAll(model.getData().getModels());
                    break;
            }
            adapter.clear();
            adapter.addAll(list);
            adapter.notifyDataSetChanged();
        } else {
            T.showToast(mContext, model.getMsg());
        }
    }

    @Override
    public void httpStatusError() {

    }


    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
